$(document).ready(function () {

    getLocation();
    $("#add").click(function () {
        var name = $("#tname").val();
        var x = $("#jd").val();
        var y = $("#wd").val();
        $.ajax({
            type: "POST",
            url: "/location/insertLocation",
            dataType: "json",
            data: {
                name: name,
                x: x,
                y: y,
            },
            success: function (data) {
                console.log(data);
                if (data == 1) {
                    alert("添加成功!!")
                }
                else {
                    alert("失败!!!")
                }

                $("#tname").val("");
                $("#jd").val("");
                $("#wd").val("");
                $("#t1 tr:not(:first)").empty();
                getLocation();
            },
            error: function (xhr, textStatus) {
                console.log('错误');
                console.log(xhr);
                console.log(textStatus);
            }
        });

    });
function getLocation() {
    $.getJSON("/location/getLocations", function (json) {
        $.each(json, function (i, item) {
            console.log("item.id" + item.id);
            $("#t1").append("<tr><td>" + item.id + "</td><td>" + item.name + "</td><td>" +
                item.x + "</td><td>" + item.y + "</td><td>" + "<input type='button' class='del' value='删除' /> " + "</td></tr>");

        });
        $(".del").click(function () {
            var id = $(this).parent().parent().find("td").eq(0).text();
            $.ajax({
                type: "POST",
                url: "/location/deleteLocation",
                dataType: "json",
                data: {
                    id: id,
                },
                success: function (data) {
                    console.log(data);
                    if (data == 1) {
                        alert("删除成功!!")
                    }
                    else {
                        alert("失败!!!")
                    }

                },
                error: function (xhr, textStatus) {
                    console.log('错误');
                    console.log(xhr);
                    console.log(textStatus);
                }
            });
            $(this).parent().parent().remove();

        });

    });

}

});




